from Accounting.CashFlow.Base import *
from DataAccess.DBConnFactory import DBConnFactory
from string import Template

CASH_CHG_INSERT_SQL = '''insert into cash_chg(ref_date,entity,amount,curncy,des,detail)
						values(TO_DATE(:1,'yyyy-mm-dd'),:2,:3,:4,:5,:6)'''

class Subscription(CashFlow):
	db_table = "share_chg"
	
	def __init__(self, ref_date, amt, curncy, conv_by, entity, detail=''):
		assert amt>0
		CashFlow.__init__(self, ref_date, amt, curncy, entity)
		self.conv_by = conv_by
		self.detail = detail
		
	def persistence(self):
		parent = self.entity.get_parent()
		
		conn = DBConnFactory().get_db_connection('ACCOUNTING')
		
		#add to share_chg table
		sql_text = '''insert into share_chg 
				(ref_date,child,parent,amount,curncy,type,conv_type,detail)
				values(TO_DATE(:1,'yyyy-mm-dd'),:2,:3,:4,:5,:6,:7,:8)'''
		conn.cursor().execute(sql_text, (self.ref_date.isoformat(), 
										self.entity.get_entity_id(), 
										parent.get_entity_id(), 
										self.amount(), 
										self.currency(),
										"subscription", 
										self.conv_by,
										self.detail))
		
		#add to cash_chg table
		data = [(self.ref_date.isoformat(),parent.get_entity_id(),self.amount(),self.currency(),"subscription",self.entity.get_entity_id()),
				(self.ref_date.isoformat(),self.entity.get_entity_id(),-self.amount(),self.currency(),"subscription",'')]										
		conn.cursor().executemany(CASH_CHG_INSERT_SQL, data)									
		conn.commit()


#as enum type
RedemptionByShare = 1
RedemptionByAmount = 2
		
class Redemption(CashFlow):
	db_table = "share_chg"
	
	def __init__(self, ref_date, amt, curncy, type, 
				corresponding_subscription_id, conv_by, entity, detail=''):
		self.type = type
		self.corr_id = corresponding_subscription_id
		self.conv_by = conv_by
		self.detail = detail
		
		if self.type==RedemptionByAmount: 
			self.shares = None
			CashFlow.__init__(self, ref_date, amt, curncy, entity)
		else:
			self.shares = amt
			CashFlow.__init__(self, ref_date, -1.0, curncy, entity)
			#self.amt = None
	
	def persistence(self):
		parent = self.entity.get_parent()
		
		#if check, using the feeder_remain_share table
		#NOT the share_chg table.		
		conn = DBConnFactory().get_db_connection('ACCOUNTING')
		
		#add to share_chg table
		if self.type==RedemptionByAmount: 
			sql_text = '''insert into share_chg 
					(ref_date,child,parent,amount,curncy,type,corr_id,conv_type,detail)
					values(TO_DATE(:1,'yyyy-mm-dd'),:2,:3,:4,:5,:6,:7,:8,:9)'''
			conn.cursor().execute(sql_text, (self.ref_date.isoformat(), 
									self.entity.get_entity_id(), 
									parent.get_entity_id(), 
									self.amount(), 
									self.currency(),
									"redemption", 
									self.corr_id,  #if feeder, None
									self.conv_by,
									self.detail))

		else:
			sql_text = '''insert into share_chg 
					(ref_date,child,parent,shares,type,corr_id,conv_type,detail)
					values(TO_DATE(:1,'yyyy-mm-dd'),:2,:3,:4,:5,:6,:7,:8)'''
			conn.cursor().execute(sql_text, (self.ref_date.isoformat(), 
									self.entity.get_entity_id(), 
									parent.get_entity_id(), 
									self.shares, 
									"redemption", 
									self.corr_id,  #if feeder, None
									self.conv_by,
									self.detail))

		#add to cash_chg table
		data = [(self.ref_date.isoformat(),parent.get_entity_id(),-self.amount(),self.currency(),"redemption",self.entity.get_entity_id()),
				(self.ref_date.isoformat(),self.entity.get_entity_id(),self.amount(),self.currency(),"redemption",'')]											
		conn.cursor().executemany(CASH_CHG_INSERT_SQL, data)
		conn.commit()
		
		
	def amount(self):
		if self.amt < 0: #NA
			cursor = DBConnFactory().get_db_connection('ACCOUNTING').cursor()
			
			sql_tpl = Template('''select avpu from asset_value_ex 
								where ref_date=TO_DATE('${DATE}','yyyy-mm-dd')
								and entity='${ENTITY}'
								and type='${TYPE}' ''')
			sql_text = sql_tpl.substitute(DATE=self.ref_date.isoformat(), 
										ENTITY=self.entity.get_parent().get_entity_id(), 
										TYPE=self.conv_by)
			cursor.execute(sql_text)
			r = cursor.fetchone()
			if r and len(r) > 0:
				avpu = r[0]
				self.amt = avpu*self.shares
			else:
				raise AVPU_NA(), ('AVPU not available, '+sql_text)
		
		return self.amt
			

		